<template>
  <n-space vertical>
    <n-flex>
      <template v-if="showDatePicker">
        <n-date-picker :disabled="commonStore.loading" v-model:value="range" type="daterange" clearable
                       class="w-280px"/>
      </template>
      <n-button :loading="commonStore.loading" :disabled="commonStore.loading" type="primary" tertiary
                @click="doAction">
        {{ props.btnLabel }}
      </n-button>
    </n-flex>
    <n-log ref="logInstRef" font-size="12" line-height="1.5" :log="logs"
           style="border: 1px solid lightgray;border-radius: 5px;padding-left: 10px;padding-top: 10px;padding-bottom: 10px"/>
  </n-space>
</template>

<script setup lang="ts">
import {LogInst, NButton} from 'naive-ui'
import {MarketService} from "../../bindings/quant/backend/service";
import {nextTick, onMounted, onUnmounted, ref, watchEffect} from "vue";
import dayjs from 'dayjs';
import {Events} from "@wailsio/runtime";
import {useCommonStore} from "../store/common";

const props = defineProps({
  action: {
    type: String,
    default: ""
  },
  btnLabel: {
    type: String,
    default: ""
  }
})
const commonStore = useCommonStore()
const logs = ref("")
const logInstRef = ref<LogInst | null>(null)
const showDatePicker = ref(true)
// 日期范围为昨天开始，昨天结束，就是昨天
// const range = ref([dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().subtract(1, 'day').format('YYYY-MM-DD')])
const range = ref([dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')])

const doAction = () => {
  logs.value = ""
  switch (props.action) {
    case 'downloadToday':
    case "downloadTomorrow":
    case "downloadCustom":
      downloadMarket()
      break;
    case 'maToday':
    case "maTomorrow":
    case "maCustom":
    case "maReset":
      calculateMAn()
      break;
    case 'macdToday':
    case "macdTomorrow":
    case "macdCustom":
    case "macdReset":
      calculateMACD();
      break;
    case 'kdjToday':
    case "kdjTomorrow":
    case "kdjCustom":
    case "kdjReset":
      calculateKDJ();
      break;
    case "calculatePreferred":
      calculatePreferred()
      break;
    default:
      window.$message?.error("错误：未确定执行内容")
  }
}

const downloadMarket = () => {
  let startDate = dayjs(range.value[0]).format('YYYYMMDD')
  let endDate = dayjs(range.value[1]).format('YYYYMMDD')
  console.log(startDate)
  console.log(endDate)
  commonStore.loading = true
  MarketService.DownloadMarket(startDate, endDate).then((result) => {
    console.log(result)
  }).catch((err) => {
    console.log("获取行情数据出现错误：" + err);
  }).finally(() => {
    commonStore.loading = false
  });
}

const calculateMAn = () => {
  let startDate = ""
  let endDate = ""
  if (props.action=="maReset") {
    startDate = ""
    endDate = ""
  } else {
    startDate = dayjs(range.value[0]).format('YYYYMMDD')
    endDate = dayjs(range.value[1]).format('YYYYMMDD')
  }
  commonStore.loading = true
  MarketService.CalculateMAn(startDate, endDate).then((result) => {
    console.log(result)
  }).catch((err) => {
    console.log("生成MAn出现错误：" + err);
  }).finally(() => {
    commonStore.loading = false
  });
}

const calculateMACD = () => {
  let startDate = ""
  let endDate = ""
  if (props.action!="macdReset") {
    startDate = dayjs(range.value[0]).format('YYYYMMDD')
    endDate = dayjs(range.value[1]).format('YYYYMMDD')
  }
  commonStore.loading = true
  MarketService.CalculateMACD(startDate, endDate).then((result) => {
    console.log(result)
  }).catch((err) => {
    console.log("生成MACD出现错误：" + err);
  }).finally(() => {
    commonStore.loading = false
  });
}

const calculateKDJ = () => {
  let startDate = ""
  let endDate = ""
  if (props.action=="kdjReset") {
    startDate = ""
    endDate = ""
  } else {
    startDate = dayjs(range.value[0]).format('YYYYMMDD')
    endDate = dayjs(range.value[1]).format('YYYYMMDD')
  }
  commonStore.loading = true
  MarketService.CalculateKDJ(startDate, endDate).then((result) => {
    console.log(result)
  }).catch((err) => {
    console.log("生成KDJ出现错误：" + err);
  }).finally(() => {
    commonStore.loading = false
  });
}

const calculatePreferred = () => {
  let startDate = dayjs(range.value[0]).format('YYYYMMDD')
  let endDate = dayjs(range.value[1]).format('YYYYMMDD')
  commonStore.loading = true
  MarketService.CalculatePreferred(startDate, endDate).then((result) => {
    console.log(result)
  }).catch((err) => {
    console.log("生成优选股票出现错误：" + err);
  }).finally(() => {
    commonStore.loading = false
  });
}

onMounted(() => {
  watchEffect(() => {
    if (logs.value) {
      nextTick(() => {
        logInstRef.value?.scrollTo({position: "bottom", silent: true});
      });
    }
  });
  showDatePicker.value = true
  switch (props.action) {
    case 'downloadToday':
      range.value = [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      break;
    case "downloadTomorrow":
      range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().subtract(1, 'day').format('YYYY-MM-DD')]
      break;
    case "downloadCustom":
      range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      break;

    case "maToday":
      range.value = [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      break;
    case "maTomorrow":
      range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().subtract(1, 'day').format('YYYY-MM-DD')]
      break;
    case "maCustom":
      range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      break;
    case "maReset":
      showDatePicker.value = false
      break;

    case "macdToday":
      range.value = [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      break;
    case "macdTomorrow":
      range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().subtract(1, 'day').format('YYYY-MM-DD')]
      break
    case "macdCustom":
      range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      break;
    case "macdReset":
      showDatePicker.value = false
      break

    case "kdjToday":
      range.value = [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      break;
    case "kdjTomorrow":
      range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().subtract(1, 'day').format('YYYY-MM-DD')]
      break;
    case "kdjCustom":
      range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
      break;
    case "kdjReset":
      showDatePicker.value = false
      break;
  }

  // if (props.action == 'calculatePreferred') {
  //   range.value = [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')]
  // } else {
  //   range.value = [dayjs().subtract(1, 'day').format('YYYY-MM-DD'), dayjs().subtract(1, 'day').format('YYYY-MM-DD')]
  // }

  Events.On('logs', (obj) => {
    logs.value += obj.data;
  });
})

onUnmounted(() => {
  Events.Off('logs');
})
</script>


<style scoped>

</style>